import { createUniver, defaultTheme, LocaleType, merge } from '@univerjs/presets'
import { UniverSheetsCorePreset } from '@univerjs/presets/preset-sheets-core'
import UniverPresetSheetsCoreZhCN from '@univerjs/presets/preset-sheets-core/locales/zh-CN'

import '@univerjs/presets/lib/styles/preset-sheets-core.css'

/**
 * Univer 初始化选项接口
 */
interface SetupUniverOptions {
  container: HTMLElement;
}

/**
 * 初始化 Univer 实例的函数
 * @param {SetupUniverOptions} options - 包含初始化所需的容器元素
 * @returns {UniverAPI} - 返回 Univer 实例的 API 对象
 */
export function setupUniver({ container }: SetupUniverOptions) {
  // 创建 Univer 实例配置
  const { univerAPI } = createUniver({
    locale: LocaleType.ZH_CN,
    locales: {
      [LocaleType.ZH_CN]: merge(
        {},
        UniverPresetSheetsCoreZhCN,
      ),
    },
    theme: defaultTheme,
    presets: [
      UniverSheetsCorePreset({
        container: container as unknown as HTMLElement, // 根据实际类型调整
      })
    ],
  })

  // 创建默认工作簿
  univerAPI.createWorkbook()

  // 返回 Univer 实例的 API 对象
  return univerAPI
}
